package cn.miaopasi.boot.clickhouse.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.miaopasi.boot.clickhouse.model.AppInfo;
import cn.miaopasi.boot.clickhouse.service.IAppInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 登录控制器
 *
 * @author lixin
 */
@RestController
@RequestMapping("/user")
public class UserLoginCtl {

    @Resource
    private IAppInfoService appInfoService;

    @PostMapping("doLogin")
    public SaResult login(
            @RequestParam(name = "secretId") String secretId,
            @RequestParam(name = "secretKey") String secretKey
    ) {
        final AppInfo appInfo = appInfoService.findBySecret(secretId, secretKey);
        if (null != appInfo) {
            StpUtil.login(appInfo.getAppId());
            return SaResult.data(StpUtil.getTokenInfo());
        }
        return SaResult.error("secretId和secretKey不匹配");
    }

    @GetMapping("isLogin")
    public SaResult isLogin() {
        return SaResult.data(StpUtil.isLogin());
    }

    @GetMapping("doLogout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }

}
